package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


import guoanmaker.operator.business.model.OperatorCustomers;
import guoanmaker.operator.business.model.OperatorMessage;
import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.model.Userview;
import guoanmaker.operator.business.repository.OperatorCustomersRepository;
import guoanmaker.operator.business.repository.OperatorMessageRepository;
import guoanmaker.operator.business.repository.OperatorUserRepository;
import guoanmaker.operator.business.repository.ProviderDictionaryRepository;
import guoanmaker.operator.helper.common.util.StringUtil;

@Service
public class OperatorMessageService implements OperatorMessageServiceIntegerface {
	@Autowired
private OperatorMessageRepository operatorMessageRepository;
	@Autowired
	private OperatorCustomersRepository operatorCustomersRepository;
	@Autowired
	private  ProviderDictionaryRepository providerDictionaryRepository;
	@Autowired
	private OperatorUserRepository operatorUserRepository;
	@Autowired
	private OperatorCustomersServiceIntegerface operatorCustomersServiceIntegerface;
	 @PersistenceContext
		private EntityManager manager;
	@Override
	@Transactional
	public OperatorMessage saveOperatorMessage(OperatorMessage operatorMessage, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		if(null!=operatorMessage.getId()&&!operatorMessage.getId().equals("")){
			if(operatorMessage.getOperatorCustomers().size()!=0){
				operatorCustomersServiceIntegerface.delectAllBymessId(operatorMessage.getId());
			}
			
			
		}else{
			
			operatorMessage.setSendStatus("1");
			
		}
		operatorMessage.setCreateTime(new Date());
		if(operatorMessage.getSendTimeType().equals("1")){
			operatorMessage.setSendTime(new Date());
		}
		OperatorMessage OpMes=operatorMessageRepository.save(operatorMessage);
		List<OperatorCustomers> cusers=operatorMessage.getOperatorCustomers();
		for(OperatorCustomers cus:cusers){
			cus.setMessId(OpMes.getId());
			
			operatorCustomersRepository.save(cus);
		}
		
		
		return OpMes;
	}
	
	public Map selectOperatorMessageListByPage(Integer page,Integer size){
		Map<String, Object> reMap = new HashMap<>();
		List<OperatorMessage> messages=operatorMessageRepository.findAll();
		reMap.put("count", messages.size());
		Pageable pageable = new PageRequest(page, size);
		Page<OperatorMessage> messpages=operatorMessageRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "createTime")));
		for(OperatorMessage operatorMessage:messpages){
			if(operatorMessage.getIfEffect().equals("1")){
				operatorMessage.setIfEffect("已生效");
			}else{
				operatorMessage.setIfEffect("未生效");
			}
			PerviderDictionary pDictionary=providerDictionaryRepository.findOne(operatorMessage.getMesType());
			if(null!=pDictionary){
				operatorMessage.setMesType(pDictionary.getDictName());
			}
			
			String[] sendways= StringUtil.StrList(operatorMessage.getSendWays());
			System.out.println(sendways.length+"长度");
			
			for(int i=0;i<sendways.length;i++){
				PerviderDictionary pd=providerDictionaryRepository.findOne(sendways[i]);
				if(pd!=null){
					sendways[i]=pd.getDictName();
				}
			}
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < sendways.length; i++){
			 sb. append(sendways[i]);
			 if(i<sendways.length-1){
			 sb.append(",");}
			}
			operatorMessage.setSendWays(sb.toString());
		}
		reMap.put("messpages", messpages);
		return reMap;
	}
	
	
	public List<OperatorMessage> selectOperatorMessageList(){
		Map<String, Object> reMap = new HashMap<>();
		List<OperatorMessage> messages=operatorMessageRepository.findByOperatorMessages();
		
		return messages;
	}
	@Transactional
	public OperatorMessage updateOperatorMessage(OperatorMessage operatorMessage){
		return operatorMessageRepository.save(operatorMessage);
	}

	@Override
	public OperatorMessage selectOperatorMessage(String id) {
		// TODO Auto-generated method stub
		OperatorMessage operatorMessage=operatorMessageRepository.findOne(id);
		List<OperatorCustomers> cus=null;
		if(operatorMessage!=null){
			cus=operatorCustomersRepository.findBymessId(operatorMessage.getId());
		}
		operatorMessage.setOperatorCustomers(cus);
		return operatorMessage;
	}

	@Override
	@Transactional
	public void deleteOperatorMessage(String id) {
		// TODO Auto-generated method stub
		operatorMessageRepository.delete(id);
	}
	
	
	
	
	

}
